package cn.edu.gdgm.system.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * Api 返回结果
 *
 * @param <T> 数据对象
 */
@Data
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResult<T> {

    private int code;

    private String msg;

    private T data;

    private static <T> RestResult<T> make(RestResultType type) {
        return make(type.getCode(), type.getDefaultMsg(), null);
    }

    private static <T> RestResult<T> make(int code, String msg, T data) {
        return new RestResult<>(code, msg, data);
    }

    public static <T> RestResult<T> ok() {
        return make(RestResultType.OK);
    }

    public static <T> RestResult<T> ok(T body) {
        return make(RestResultType.OK.getCode(), RestResultType.OK.getDefaultMsg(), body);
    }

    public static <T> RestResult<T> error(RestResultType type) {
        return make(type);
    }

    public static <T> RestResult<T> error(RestResultType type, String errorMsg) {
        return make(type.getCode(), errorMsg, null);
    }
}
